﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SmoothStreaming.PerformanceTesting.WinUI
{
    public partial class MainForm : Form
    {
        public static DeskMetrics.Watcher DMWatcher = new DeskMetrics.Watcher();

        public MainForm()
        {
            InitializeComponent();

            this.Load += new EventHandler(OnLoad);
            this.FormClosing += new FormClosingEventHandler(OnClosing);
        }

        void OnClosing(object sender, FormClosingEventArgs e)
        {
            try
            {
                DMWatcher.Stop();
            }
            catch { }
        }

        void OnLoad(object sender, EventArgs e)
        {
            this.tabs.TabPages.Clear();

            try
            {
                DMWatcher.Start("4e25d9038d15fc311e000000", "1.0");
            }
            catch { }
        }

        void OnAppInstanceCompleted(object sender, AsyncCompletedEventArgs e)
        {
            
        }

        private void btnBeginTest_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(this.txtManifestUrl.Text))
            {
                MessageBox.Show("Manifest URL required.", "Manifest URL");
                return;
            }

            Uri manifestUri = new Uri(this.txtManifestUrl.Text);

            if (!manifestUri.Scheme.StartsWith("http"))
            {
                MessageBox.Show("Manifest URL must start with HTTP.", "Manifest URL");
                return;
            }

            DMWatcher.TrackEvent("ButtonClick", "BeginTestClicked");

            var ismFileName = manifestUri.Segments.Where(s => s.Contains(".ism")).FirstOrDefault();
            var domain = manifestUri.DnsSafeHost;

            if (string.IsNullOrWhiteSpace(ismFileName))
            {
                ismFileName = "manifest";
            }

            ismFileName = ismFileName.Replace("/", string.Empty);
            ismFileName = ismFileName.Replace("%20", " ");

            TabPage tp = new TabPage();
            tp.Text = ismFileName + " on " + domain;

            tabs.TabPages.Add(tp);

            TestViewControl tvc = new TestViewControl();
            tp.Controls.Add(tvc);
            tvc.Dock = DockStyle.Fill;

            tvc.StartManifestTest(manifestUri);
        }

        private void btnClearTests_Click(object sender, EventArgs e)
        {
            DMWatcher.TrackEvent("ButtonClick", "ClearTestsClicked");
            foreach (TabPage tp in this.tabs.TabPages)
            {
                try
                {
                    var tvc = tp.Controls[0] as TestViewControl;

                    tvc.PerformanceTest.Stop();
                }
                catch { }
            }

            this.tabs.TabPages.Clear();
        }
    }
}
